package net.huashitong.Project.service;

import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.responseModel.ProcessResultModel;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class NodeNewschedulesProcess extends StringDao<NodeNewschedules> {

    @Autowired
    private NodeNewschedulesService nss;
    @Autowired
    private NewProjectsService newProjectsService;

    public ProcessResultModel getjj(String projectId,String dept){
        ProcessResultModel processResultModel = new ProcessResultModel();
        //判断项目id
        if(StringUtils.isNotBlank(projectId)){
            NewProjectsFilter filter = new NewProjectsFilter();
            filter.setSysId(projectId);
            List<NewProjects> projectInfos = newProjectsService.getProjectInfo(filter);
            if(StringUtils.isNotBlank(dept)){
                //获取相应部门的所有小节点
                List<NodeNewschedules> smallNodes = nss.queryListByProjectId(projectId,"2",dept,"1",null);
                if(smallNodes.size()==0){
                    return null;
                }
                int begin = 0;          //已开始数
                int nobegin = 0;        //未开始数

                int finished = 0;       //已完成数
                int noFinish = 0;       //未完成

                int countFinishDays = 0;    //总完成天数(实际用时)
                int beyondFinishedDays = 0; //超前完成天数
                int planTimes = 0;

                String nodeId;          //节点id
                String nodeName;        //节点名称
                int beyond = 0;         //超前
                int normal = 0;         //正常
                int late = 0;           //逾期
                for(NodeNewschedules smallNode:smallNodes){
                    if(StringUtils.isNotBlank(smallNode.getPlanTime())){
                        planTimes = planTimes + Integer.parseInt(smallNode.getPlanTime());
                    }
                    //判断已完成
                    if("2".equals(smallNode.getStatus())){
                        finished++;
                        String planTime = smallNode.getPlanTime();  //计划时间
                        String factTime =smallNode.getFactTime();   //实际时间
                        if(StringUtils.isNotBlank(planTime)){
                            if(StringUtils.isNotBlank(factTime)){

                                //实际时间-计划时间
                                //总完成天数
                                countFinishDays = countFinishDays + Integer.parseInt(factTime);

                                if(Integer.parseInt(factTime)-Integer.parseInt(planTime)>0){        //逾期
                                    late++;
                                }else if(Integer.parseInt(factTime)-Integer.parseInt(planTime)<0){  //超前
                                    beyondFinishedDays = beyondFinishedDays + (Integer.parseInt(planTime)-Integer.parseInt(factTime));
                                    beyond++;
                                }else if(Integer.parseInt(factTime)-Integer.parseInt(planTime)==0){ //正常
                                    normal++;
                                }
                            }else{
                                //没有实际时间
                                nobegin++;
                            }
                        }else{
                            //没有计划时间
                            nobegin++;
                        }

                    }else if("1".equals(smallNode.getStatus())){    //已开始
                        begin++;
                        noFinish++;
                    }else if("0".equals(smallNode.getStatus())){    //未开始
                        nobegin++;
                        noFinish++;
                    }else if(StringUtils.isBlank(smallNode.getStatus())){

                        noFinish++;
                    }
                }
                processResultModel.setPlanTimes(planTimes);
                processResultModel.setCountFinishDays(countFinishDays);
                processResultModel.setBeyondFinishedDays(beyondFinishedDays);
                processResultModel.setAllNode(smallNodes.size());
                processResultModel.setBegin(begin);
                processResultModel.setBeyond(beyond);
                processResultModel.setFinished(finished);
                processResultModel.setLate(late);
                processResultModel.setNobegin(nobegin);
                processResultModel.setNoFinish(noFinish);
                processResultModel.setNormal(normal);
                processResultModel.setNodeId(smallNodes.get(0).getNodeId());
                processResultModel.setNodeId(smallNodes.get(0).getNodeName());
                processResultModel.setProjectId(projectId); //设置项目ID
                processResultModel.setDept(dept);           //设置部门
                processResultModel.setProjectName(projectInfos.get(0).getName());

            }else{      //统计整个项目，不区分部门
                List<NodeNewschedules> smallNodes = nss.queryListByProjectId(projectId,"2",null,"1",null);
                if(smallNodes.size()==0){
                    return null;
                }
                int begin = 0;          //已开始数
                int nobegin = 0;        //未开始数

                int finished = 0;       //已完成数
                int noFinish = 0;       //未完成

                int countFinishDays = 0;    //总完成天数
                int beyondFinishedDays = 0; //超前完成天数
                int planTimes = 0;          //

                String nodeId;          //节点id
                String nodeName;        //节点名称
                int beyond = 0;         //超前
                int normal = 0;         //正常
                int late = 0;           //逾期
                for(NodeNewschedules smallNode:smallNodes){

                    if(StringUtils.isNotBlank(smallNode.getPlanTime())){
                        planTimes = planTimes + Integer.parseInt(smallNode.getPlanTime());
                    }

                    //判断已完成
                    if("2".equals(smallNode.getStatus())){
                        finished++;
                        String planTime = smallNode.getPlanTime();  //计划时间
                        String factTime =smallNode.getFactTime();   //实际时间
                        if(StringUtils.isNotBlank(planTime)){
                            if(StringUtils.isNotBlank(factTime)){

                                //实际时间-计划时间
                                int f = Integer.parseInt(factTime);
                                int p = Integer.parseInt(planTime);

                                //总完成天数
                                countFinishDays = countFinishDays + f;
                                if(f-p>0){        //逾期
                                    late++;
                                }else if(f-p<0){  //超前
                                    beyondFinishedDays = beyondFinishedDays + (p-f);
                                    beyond++;
                                }else if(f-p==0){ //正常
                                    normal++;
                                }
                            }else{
                                //没有实际时间
                                nobegin++;
                            }
                        }else{
                            //没有计划时间
                            nobegin++;
                        }
                    }else if("1".equals(smallNode.getStatus())){    //已开始
                        begin++;
                        noFinish++;
                    }else if("0".equals(smallNode.getStatus())){    //未开始
                        nobegin++;
                        noFinish++;
                    }else if(StringUtils.isBlank(smallNode.getStatus())){
                        //Status为空
                        noFinish++;
                    }
                }
                processResultModel.setPlanTimes(planTimes);
                processResultModel.setCountFinishDays(countFinishDays);
                processResultModel.setBeyondFinishedDays(beyondFinishedDays);
                processResultModel.setAllNode(smallNodes.size());
                processResultModel.setBegin(begin);
                processResultModel.setBeyond(beyond);
                processResultModel.setFinished(finished);
                processResultModel.setLate(late);
                processResultModel.setNobegin(nobegin);
                processResultModel.setNoFinish(noFinish);
                processResultModel.setNormal(normal);
                processResultModel.setNodeId(smallNodes.get(0).getNodeId());
                processResultModel.setNodeId(smallNodes.get(0).getNodeName());
                processResultModel.setProjectId(projectId); //设置项目ID
                processResultModel.setDept(null);           //设置部门
                processResultModel.setProjectName(projectInfos.get(0).getName());
            }
        }else{
            //项目Id为空

        }
        return processResultModel;
    }
}
